﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace SolutionCrapFinder
{
    public partial class MainForm : Form
    {
        private void ShowCrapFiles()
        {
            crapFileListBox.Items.Clear();
            FileFilter filter = new FileFilter(textBox2.Text);
            foreach (string s in m_allCrapFiles)
            {
                bool add = true;
                if (m_settings.m_ignoredFiles.Contains(s))
                {
                    add = false;
                }
                if (add && !filter.IsFilterMatched(s))
                {
                    add = false;                    
                }

                if (add)
                {
                    crapFileListBox.Items.Add(s);
                }
            }
        }

        private void SaveProject(bool saveAs)
        {
            bool showSaveDlg = saveAs || (m_fileSettings == null);

            if (showSaveDlg)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "SCF Project files (*.scfproj)|*.scfproj";
                dlg.ShowDialog();
                if (File.Exists(dlg.FileName))
                {
                    DialogResult dlgRes = MessageBox.Show("File already exists. Overwrite?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (dlgRes == DialogResult.Cancel)
                        return;

                    File.Delete(dlg.FileName);
                }
                m_fileSettings = dlg.FileName;
            }
            
            SCFProjectSettings.Save(m_fileSettings, m_settings);
        }

        private void CloseProject()
        {
            if (m_settings.m_IsDirty)
            {
                DialogResult res = MessageBox.Show("Would you like to save current project?", this.Text, MessageBoxButtons.OKCancel);
                if (res == DialogResult.OK)
                {
                    SaveProject(false);
                }
            }
            m_settings.Clear();
        }

        private void FillRecentProjects()
        {

        }

        private void PrepareCrapFileListContextMenu()
        {
            listboxContextMenu = new ContextMenuStrip();
            listboxContextMenu.Opening += new CancelEventHandler(listboxContextMenu_Opening);
            crapFileListBox.ContextMenuStrip = listboxContextMenu;
            listboxContextMenu.ItemClicked += new ToolStripItemClickedEventHandler(listboxContextMenu_ItemClicked);
        }
    }
}